﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier_Service;
using Cashier_Model;
using System.Collections;
using Cashier.utils;

namespace Cashier.finance
{
    public partial class PurchasePaymentAddForm : Form
    {
        private MainForm mainform;
        private IReloadData iReloadData;
        private DataGridViewRow dgvRow;
        private BillPurchasePaymentService purchasePaymentService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="dgvRow"></param>
        /// <param name="debtAmount"></param>
        /// <param name="iReloadData"></param>
        public PurchasePaymentAddForm(MainForm mainform, DataGridViewRow dgvRow, decimal debtAmount, IReloadData iReloadData)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dgvRow = dgvRow;
            this.iReloadData = iReloadData;
            this.purchasePaymentService = new BillPurchasePaymentService();

            this.ControlsSetRights();
            label_debtAmount.Text = debtAmount.ToString("f2");
            this.InitialFormData(debtAmount);
            textbox_payAmount.LostFocus += new EventHandler(textbox_payAmount_LostFocus);
        }

        #region 功能方法实现

        /// <summary>
        /// 控件设置权限
        /// </summary>
        private void ControlsSetRights()
        {
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
        }

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        /// <param name="debtAmount"></param>
        private void InitialFormData(decimal debtAmount)
        {
            if (dgvRow != null)
            {
                label_purchaseno.Text = dgvRow.Cells["purchase_no"].Value.ToString();
                String supplierno = dgvRow.Cells["supplier_no"].Value.ToString();
                label_supplierno.Text = supplierno;
                label_supplierName.Text = dgvRow.Cells["supplier_name"].Value.ToString();
                label_purchaseTotal.Text = ((decimal)dgvRow.Cells["purchase_total"].Value).ToString("f2");
                label_debtAmount.Text = debtAmount.ToString("f2");

                Supplier supplier = new SupplierService().GetSupplier(0, supplierno);
                if (supplier != null)
                {
                    textbox_bankName.Text = supplier.BankName;
                    textbox_bankAccount.Text = supplier.BankAccount;
                    textbox_payee.Text = supplier.Payee;
                }
            }
        }

        /// <summary>
        /// 支付结果
        /// </summary>
        private bool PayResult()
        {
            bool result = true;
            decimal purchaseTotal = Decimal.Parse(label_purchaseTotal.Text);
            decimal debtAmount = Decimal.Parse(label_debtAmount.Text);
            decimal payAmount = String.IsNullOrEmpty(textbox_payAmount.Text.Trim())?0:Decimal.Parse(textbox_payAmount.Text.Trim());
            decimal remainAmount = 0M;

            label_remainAmount.Text = "0";
            if (debtAmount == 0)
            {
                MessageBox.Show("采购单已经结清！");
                return result = false;
            }
            if (payAmount > debtAmount)
            {
                MessageBox.Show("支付金额不得超过所欠金额！");
                result = false;
                textbox_payAmount.Focus();
            }
            else if (payAmount < debtAmount)
            {
                remainAmount = debtAmount - payAmount;
                label_remainAmount.Text = remainAmount.ToString("f2");
            }
            return result;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_payAmount_LostFocus(object sender, EventArgs e)
        {
            this.PayResult();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            FormUtil.GetValidateControlsNotPassed(this.groupBox1, list, 1);
            if (list.Count > 0)
                return;
            
            decimal purchaseTotal = Decimal.Parse(label_purchaseTotal.Text);
            decimal debtAmount = Decimal.Parse(label_debtAmount.Text);
            decimal payAmount = String.IsNullOrEmpty(textbox_payAmount.Text.Trim())?0:Decimal.Parse(textbox_payAmount.Text.Trim());

            if (this.PayResult())
            {
                BillPurchasePayment purchasePayment = new BillPurchasePayment();
                purchasePayment.EnterpriseNo = mainform.Admin.EnterpriseNo;
                purchasePayment.StoreNo = mainform.Admin.StoreNo;
                purchasePayment.PurchaseNo = label_purchaseno.Text;
                purchasePayment.SupplierNo = label_supplierno.Text;
                purchasePayment.SupplierName = label_supplierName.Text;
                purchasePayment.PurchaseTotal = purchaseTotal;
                purchasePayment.DebtAmount = debtAmount;
                purchasePayment.PayAmount = payAmount;
                purchasePayment.RemainAmount = Decimal.Parse(label_remainAmount.Text);
                purchasePayment.PaymentDate = DateTime.Now;
                purchasePayment.AdminUsername = mainform.Admin.Username;
                purchasePayment.AdminName = mainform.Admin.Name;
                purchasePayment.BankName = textbox_bankName.Text.Trim();
                purchasePayment.BankAccount = textbox_bankAccount.Text.Trim();
                purchasePayment.Payee = textbox_payee.Text.Trim();
                purchasePayment.Createtime = DateTime.Now;

                long id = this.purchasePaymentService.InsertBillPurchasePayment(purchasePayment);
                if (id > 0)
                {
                    MessageBox.Show("保存成功！");
                    iReloadData.ReloadDataGridViewData(null);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }
        }

        #endregion


        #region 方法重写

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
            return b;
        }

        #endregion
    }
}
